<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/4/29
 * Time: 下午4:12
 */

namespace Home\Controller;


use Think\View;

class ArticleController extends HomeController
{
    /**
     * 频道页面
     * @return bool
     */
    public function index()
    {
        return true;
    }

    /**
     * 列表页面
     */
    public function lists()
    {
        list($list, $template) = $this->getList();

        $view = new View();
        if (empty($template) || !is_file($view->parseTemplate($template))) {
            $template = CONTROLLER_NAME.'/lists';
            if (!is_file($view->parseTemplate($template))) {
                $template = 'Article/lists';
            }
        }

        if (method_exists($this, 'afterLists')){
            $this->afterLists($list);
        }

        $this->assign('list', $list);

        $this->display($template);
    }

    /**
     * 详情页链接
     * @param int $id
     */
    public function detail($id = 0)
    {
        $detailId = I('id', $id);

        $detail = D('Document')->detail($detailId);

        if (empty($detail)) {
            $this->error('该信息不存在', U('index'));
        }

        //  获取分类信息设置seo
        $categoryInfo = D('Category')->info($detail['category_id']);

        if ($categoryInfo['meta_title']) {
            $this->seo['title'] = $categoryInfo['meta_title'];
        }

        if ($categoryInfo['keywords']) {
            $this->seo['keywords'] = $categoryInfo['keywords'];
        }

        if ($categoryInfo['description']) {
            $this->seo['description'] = $categoryInfo['description'];
        }
        $this->seo['title'] = $detail['title'];
        $this->seo['description'] = $detail['description'] ? $detail['description'] : '';
        $this->assign('seo', $this->seo);
        $this->assign('categoryInfo', $categoryInfo);

        if (method_exists('afterDetail', $this)){
            $this->afterDetail($detail);
        }

        //  设置模版
        $view = new View();
        $template = $categoryInfo['template_detail'];
        if (empty($template) || !is_file($view->parseTemplate($template))) {
            $template = CONTROLLER_NAME.'/detail';
            if (!is_file($view->parseTemplate($template))) {
                $template = 'Article/detail';
            }
        }

        $this->assign('detail', $detail);

        //  右侧栏
        //  相关课程推荐
        $curriculaCateList = D('Document')->getListByPosition([9,10,11], 4, 8);
        $this->assign('curriculaList', $curriculaCateList);

        //  学校活动
        $activityList = D('Document')->getListByPosition(8, 4, 3);
        $this->assign('activityList', $activityList);


        //  获取广告
        $parentCategoryInfo = D('Category')->info($categoryInfo['pid']);
        if($parentCategoryInfo['pid'] == 0) {
            $rootCategoryInfo = $parentCategoryInfo;
        } else {
            $rootCategoryInfo = D('Category')->info($parentCategoryInfo['pid']);
        }
        $this->assign('bannerList', D('Advertise')->getAdvertiseListByOnlySign(strtolower($rootCategoryInfo['name'])));


        $this->display($template);
    }

    /**
     * 获取列表数据
     * @param int $cateid
     * @return array
     */
    protected function getList($cateid = 0)
    {
        $cateid = I('cateid', $cateid);

        if (empty($cateid)) {
            $this->error('缺少必要参数');
        }

        $categoryInfo = D('Category')->info($cateid);

        $childCategoryList = D('Category')->getListByPid($categoryInfo['id']);
        if ($childCategoryList) {
            $categoryInfo = $childCategoryList[0];
        }
        $cateid = $categoryInfo['id'];

        //  同一级分类的列表
        $categoryList = D('Category')->getListByPid($categoryInfo['pid']);
        //  同一级分类的上级分类列表
        $parentCategoryInfo = D('Category')->info($categoryInfo['pid']);
        $rootid = $parentCategoryInfo['pid'] ? $parentCategoryInfo['pid'] : 0;
        if (empty($rootid)) {
            $parentCategoryList = $categoryList;
            $parentCategoryInfo = $categoryInfo;
            $categoryList = [];
        } else {
            $parentCategoryList = D('Category')->getListByPid($rootid);
        }

        $this->assign('categoryInfo', $categoryInfo);
        $this->assign('categoryList', $categoryList);
        $this->assign('parentCategoryList', $parentCategoryList);
        $this->assign('parentCategoryInfo', $parentCategoryInfo);

        //  获取广告
        if($parentCategoryInfo['pid'] == 0) {
            $rootCategoryInfo = $parentCategoryInfo;
        } else {
            $rootCategoryInfo = D('Category')->info($parentCategoryInfo['pid']);
        }
        $this->assign('bannerList', D('Advertise')->getAdvertiseListByOnlySign(strtolower($rootCategoryInfo['name'])));

        //  设置分类seo信息
        if ($categoryInfo['meta_title']) {
            $this->seo['title'] = $categoryInfo['meta_title'];
        }

        if ($categoryInfo['keywords']) {
            $this->seo['keywords'] = $categoryInfo['keywords'];
        }

        if ($categoryInfo['description']) {
            $this->seo['description'] = $categoryInfo['description'];
        }

        $this->assign('seo', $this->seo);

        $model = D('Document');

        $map['category_id'] = $cateid;

        return [$this->getListsPage($model, $map, 'level DESC, id DESC'), $categoryInfo['template_lists']];
    }
}